package com.whfc.fse.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.whfc.common.exception.BizException;
import com.whfc.common.result.ListData;
import com.whfc.common.result.PageData;
import com.whfc.common.result.ResultEnum;
import com.whfc.common.util.PageUtil;
import com.whfc.fse.dao.FseCraneRecordMapper;
import com.whfc.fse.dao.FseLiftRecordMapper;
import com.whfc.fse.dto.*;
import com.whfc.fse.entity.FseCraneRecord;
import com.whfc.fse.entity.FseLiftRecord;
import com.whfc.fse.enums.FseLiftSide;
import com.whfc.fse.service.FseRecordService;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @DESCRIPTION
 * @AUTHOR GuoDong_Sun
 * @DATE 2020/4/9
 */
@DubboService(interfaceClass = FseRecordService.class, version = "1.0.0", timeout = 5000)
public class FseRecordServiceImpl implements FseRecordService {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private FseCraneRecordMapper fseCraneRecordMapper;

    @Autowired
    private FseLiftRecordMapper fseLiftRecordMapper;

    @Override
    public PageData<FseCraneRecordDTO> craneRecordList(Integer deptId, Integer pageNum, Integer pageSize, Date startTime, Date endTime, String keyword) {
        logger.info("吊装记录列表服务,deptId:{},pageNum:{},pageSize:{},startTime:{},endTime:{},keyword:{}", deptId, pageNum, pageSize, startTime, endTime, keyword);
        PageHelper.startPage(pageNum, pageSize);
        List<FseCraneRecordDTO> list = fseCraneRecordMapper.selectByDeptIdAndTime(deptId, startTime, endTime, keyword);
        PageHelper.clearPage();

        return PageUtil.pageData(PageInfo.of(list));
    }

    @Override
    public ListData<OpenApiFseCraneRecordDTO> craneRecordList(String sn, Date startTime, Date endTime) throws BizException {
        logger.info("吊装记录列表服务,sn:{},startTime:{},endTime:{}", sn, startTime, endTime);
        if ((Double.valueOf(endTime.getTime() - startTime.getTime()) / 3600000) > 1.0) {
            throw new BizException(ResultEnum.PARAM_ERROR.getCode(), "时间范围最大为1小时");
        }
        List<OpenApiFseCraneRecordDTO> list = fseCraneRecordMapper.selectBySnsAndTime(Arrays.asList(sn.split(",")), startTime, endTime);
        return new ListData<>(list);
    }

    @Override
    public PageData<FseLiftRecordDTO> liftRecordList(Integer deptId, Integer pageNum, Integer pageSize, Date startTime, Date endTime, String keyword) {
        logger.info("升降机记录列表服务,deptId:{},pageNum:{},pageSize:{},startTime:{},endTime:{},keyword:{}",
                deptId, pageNum, pageSize, startTime, endTime, keyword);
        PageHelper.startPage(pageNum, pageSize);
        List<FseLiftRecordDTO> list = fseLiftRecordMapper.selectByDeptIdAndTime(deptId, startTime, endTime, keyword);
        PageHelper.clearPage();

        return PageUtil.pageData(PageInfo.of(list));
    }

    @Override
    public ListData<OpenApiFseLiftRecordDTO> liftRecordList(String sn, Date startTime, Date endTime) throws BizException {
        logger.info("升降机记录列表服务,sn:{},startTime:{},endTime:{}", sn, startTime, endTime);
        if (Double.valueOf(endTime.getTime() - startTime.getTime()) / 3600000 > 1) {
            throw new BizException(ResultEnum.PARAM_ERROR.getCode(), "时间范围最大为1小时");
        }
        List<OpenApiFseLiftRecordDTO> list = fseLiftRecordMapper.selectBySnsAndTime(Arrays.asList(sn.split(",")), startTime, endTime);
        return new ListData<>(list);
    }

    @Override
    public FseLiftRecordCountDTO recordCount(Integer liftId, Date startTime, Date endTime) {
        logger.info("升降次数统计,liftId:{}，startTime:{},endTime:{}", liftId, startTime, endTime);
        FseLiftRecordCountDTO fseLiftRecordCountDTO = new FseLiftRecordCountDTO();
        Integer leftCount = fseLiftRecordMapper.recordCount(liftId, FseLiftSide.LEFT.getValue(), startTime, endTime);
        Integer rightCount = fseLiftRecordMapper.recordCount(liftId, FseLiftSide.RIGHT.getValue(), startTime, endTime);
        fseLiftRecordCountDTO.setLeftSideCount(leftCount);
        fseLiftRecordCountDTO.setRightSideCount(rightCount);
        fseLiftRecordCountDTO.setTotal(leftCount + rightCount);
        return fseLiftRecordCountDTO;
    }
}
